package order.servlet;

import lombok.SneakyThrows;
import order.dao.managerDao;
import order.dao.storeDao;
import order.pojo.manager;
import order.pojo.store_information;
import order.utils.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;

public class storeServlet extends HttpServlet {
    DbUtil dbUtil = new DbUtil();
 storeDao storeDao=new storeDao();
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("=>进入了get请求");
        HttpSession session = req.getSession();

        String action = req.getParameter("action");
        if ("querry".equals(action)) {
            selectStore(req, resp);
            return;
        } else if ("update".equals(action)) {
        updatestore(req,resp);
            return;
        }
    }



    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();

        String action = req.getParameter("action");
        System.out.println("=>进入了post请求");
        if ("querry".equals(action)) {
            selectStore(req, resp);
            return;
        } else if ("update".equals(action)) {
            updatestore(req,resp);
            return;
        }
    }

    //显示所有顾客
    private void selectStore(HttpServletRequest req,HttpServletResponse resp){
        Connection con = null;
        store_information store=new store_information();

        try {

            con = dbUtil.getCon();

           store_information store_information=new store_information();
        storeDao storeDao=new storeDao();
        store=storeDao.selectStoreById(con,store);
           req.setAttribute("store",store);
            req.getRequestDispatcher("manager/update.jsp").forward(req,resp);



        } catch (Exception e) {

        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }


    //修改
    private void updatestore(HttpServletRequest req,HttpServletResponse resp) throws Exception {
        System.out.println("=>进入了修改servlet");
        store_information store=null;
        int storeId=1;
        String store_name=req.getParameter("store_name");
        String store_ad=req.getParameter("store_ad");
        String store_location=req.getParameter("store_location");
        String store_publisher=req.getParameter("store_publisher");

           store=new store_information(storeId,store_name,store_ad,store_location,store_publisher);




        Connection con=null;
        con=dbUtil.getCon();
        store=storeDao.updateStoreById(con,store);
        String messge="上传店面信息成功!";
        req.setAttribute("messge",messge);

        req.getRequestDispatcher("store?action=querry").forward(req,resp);

    }
    //增加





}

